package com.ye.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.ye.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2022年04月07日 16:31
 */
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @Autowired
    private Student student;

    @RequestMapping("/get")
    public boolean get() {
        return useLocalCache;
    }

    @GetMapping("/student")
    @SentinelResource("student")
    public String getStudent(HttpServletRequest request) {
        int port = request.getServerPort();
        String servletPath = request.getServletPath();
        String serverName = request.getServerName();
        String api = serverName + ":" + port + servletPath;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("API", api);
        jsonObject.put("student", student);
        return JSONObject.toJSONString(jsonObject);
    }
}
